package com.ky.manage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ky.manage.R;
import com.ky.manage.model.response.MountingsResp;

import java.util.List;

public class MountingsAdapter extends BaseAdapter {

    List<MountingsResp.RowsDTO> mMountings;
    Context mContext;

    public MountingsAdapter(Context context, List<MountingsResp.RowsDTO> mountings) {
        mContext = context;
        mMountings = mountings;
    }

    @Override
    public int getCount() {
        return mMountings.size();
    }

    @Override
    public Object getItem(int position) {
        return mMountings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_product, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.iv_prduct_img);
            viewHolder.tvProductName = convertView.findViewById(R.id.tv_product_name);
            viewHolder.tvProductPrice = convertView.findViewById(R.id.tv_product_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MountingsResp.RowsDTO mountings = mMountings.get(position);

        Glide.with(mContext).load(mountings.getPicUrl()).error(R.mipmap.ic_launcher).into(viewHolder.imageView);
        viewHolder.tvProductName.setText(mountings.getMountingsName());
        viewHolder.tvProductPrice.setText("¥"+mountings.getUnitPrice());
        return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView tvProductName;
        TextView tvProductPrice;
    }
}
